<template>
  <div v-show="visible" class="context-menu" :style="positionStyle" @click.stop>
    <div class="context-menu-item" @click="openDialog">上传</div>
    <!-- <div class="context-menu-item" @click="doDelete(props.data)">删除</div> -->
  </div>
  <el-dialog v-model="dialogVisible" title="上传" width="400px" @close="resetDialog">
    <el-form :model="form">
      <el-form-item label="公司名称">
        <el-autocomplete
          v-model="form.input"
          value-key="company_name"
          :fetch-suggestions="querySearch"
          placeholder="请输入内容"
          @select="handleSelect"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="handleUpload">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { computed, ref, PropType } from 'vue'
import { getCompanyNamesApi } from '@/api/company'
import { type getCompanyNamesItem } from '@/api/company/types'
import { doUpdateByCompanyApi } from '@/api/quote'
// import { doDeleteApi, doUpdateByCompanyApi } from '@/api/quote'
import { type QuoteRecord } from '@/api/quote/types'
import daysjs from 'dayjs'

// const uid = ref<number>(0)
const company_id = ref<number>(0)

interface Position {
  x: number
  y: number
}

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  position: {
    type: Object as PropType<Position>,
    required: true
  },
  data: {
    type: Object as PropType<QuoteRecord>,
    required: true
  }
})

const emit = defineEmits(['upload', 'delete'])

const positionStyle = computed(() => ({
  left: `${props.position.x}px`,
  top: `${props.position.y}px`
}))
const dialogVisible = ref(false)
const form = ref({ input: '' })

function openDialog() {
  dialogVisible.value = true
}
function resetDialog() {
  form.value.input = ''
}
function handleUpload() {
  doUpdateByCompany(props.data)
  // emit('upload', form.value.input)
  dialogVisible.value = false
  resetDialog()
}

async function querySearch(queryString: string, cb: (arg: getCompanyNamesItem[]) => void) {
  const results = await getCompanyNamesApi({ company_name: queryString })
  console.log(results)
  if (results.code === 200) {
    const data = results.data
    if (data.length === 0) {
      cb([])
      return
    } else {
      cb(data)
    }
  } else {
    cb([])
  }
}

const handleSelect = (item: getCompanyNamesItem) => {
  console.log('value')
  console.log(item)
  company_id.value = item.id
  form.value.input = item.company_name
}
// const doDelete = async (item: QuoteRecord) => {
//   console.log(item)
//   const result = await doDeleteApi({ id: item.id })

//   if (result.code == 200) {
//     emit('delete')
//   }
// }
const currentDate = daysjs()
const doUpdateByCompany = async (item: QuoteRecord) => {
  let value = currentDate.format('HH:mm')
  value = value + '-' + form.value.input
  const result = await doUpdateByCompanyApi({
    id: item.id,
    company_id: company_id.value,
    company_name: form.value.input,
    title: value
  })
  if (result.code == 200) {
    emit('upload', true)
  }
  console.log(result)
}
</script>

<style scoped>
.context-menu {
  position: fixed;
  background: white;
  border: 1px solid #eee;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  padding: 5px 0;
  z-index: 9999;
}

.context-menu-item {
  padding: 8px 16px;
  cursor: pointer;
  font-size: 14px;
}

.context-menu-item:hover {
  background-color: #f5f7fa;
}
</style>
